home *** CD-ROM | disk | FTP | other *** search
Text File | 1994-09-02 | 6.6 KB | 329 lines | [TEXT/KAHL] |
- /*****
- * CPerson.cp
- *
- * Implementation of application-specific persistent classes.
- *
- * This sample app is derived from a contribution originally made
- * by Paul Gee (Thanks Paul!).
- *
- * Copyright © 1994 NeoLogic Systems. All rights reserved.
- *
- *****/
-
- #include "NeoTypes.h"
- #include CNeoDatabaseH
- #include CNeoStreamH
- #include CNeoIDListH
- #include CNeoPartListIteratorH
- #include <string.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include "CPerson.h"
-
- /* ****************************************************************** */
- /** CPerson Class **/
- /* ****************************************************************** */
- CPerson::CPerson(const CNeoString &aName)
- {
- fName = aName;
- setListClassID(kNeoIDListID);
- }
-
- #pragma segment NeoRead
- void CPerson::readObject(CNeoStream *aStream, const NeoTag aTag)
- {
- inherited::readObject(aStream, aTag);
-
- aStream->readNativeString(fName, sizeof(fName));
- }
-
- #pragma segment NeoWrite
- void CPerson::writeObject(CNeoStream *aStream, const NeoTag aTag)
- {
- inherited::writeObject(aStream, aTag);
-
- aStream->writeNativeString(fName, sizeof(fName));
- }
-
- void CPerson::printName(void) const
- {
- char name[64];
-
- strcpy(name, fName);
- printf("Name is %s\n", name);
- }
-
- /* ****************************************************************** */
- /** CJoke Class **/
- /* ****************************************************************** */
- CJoke::CJoke(const char *aText)
- {
- setJoke(aText);
- }
-
- CNeoPersist *CJoke::New(void)
- {
- return new CJoke();
- }
-
- NeoID CJoke::getClassID(void) const
- {
- return kJokeID;
- }
-
- long CJoke::getFileLength(void) const
- {
- return kNeoPersistFileLength + kMaxJokeLength;
- }
-
- #pragma segment NeoRead
- void CJoke::readObject(CNeoStream *aStream, const NeoTag aTag)
- {
- inherited::readObject(aStream, aTag);
-
- aStream->readString(fJoke, sizeof(fJoke));
- }
-
- #pragma segment NeoWrite
- void CJoke::writeObject(CNeoStream *aStream, const NeoTag aTag)
- {
- inherited::writeObject(aStream, aTag);
-
- aStream->writeString(fJoke, sizeof(fJoke));
- }
-
- void CJoke::printJoke(void) const
- {
- char joke[64];
-
- strcpy(joke, fJoke);
- printf("%s\n", joke);
- }
-
- /* ****************************************************************** */
- /** CJoker Class **/
- /* ****************************************************************** */
- CJoker::CJoker(const CNeoString &aName)
- : CPerson(aName)
- {
- setObjClassID(kJokeID);
- }
-
- CNeoPersist *CJoker::New(void)
- {
- return new CJoker();
- }
-
- NeoID CJoker::getClassID(void) const
- {
- return kJokerID;
- }
-
- long CJoker::getFileLength(void) const
- {
- return kPersonFileLength;
- }
-
- void CJoker::skill(void)
- {
- long count = getJokeCount();
- CJoke * joke;
-
- if (count) {
- // Randomly pick a joke
- joke = getJoke((rand()&0x7FFFFFFF) % count);
- NeoAssert(joke);
-
- printf("Tells jokes : ");
- joke->printJoke();
-
- // Don't forget to remove our reference to the joke.
- joke->unrefer();
- }
- else
- printf("Has no jokes to tell.\n");
- }
-
- void CJoker::forgetJoke(CJoke *aJoke)
- {
- // Remove the joke from this joker's part list
- deleteFromList(aJoke);
-
- // Note: This joke is still in the database!
- // To remove the joke completely, we would say...
- // if (fMark)
- // gNeoDatabase->removeObject(joke);
- }
-
- CJoke *CJoker::getJoke(const long aIndex)
- {
- CJoke * joke;
- CNeoPartListIterator iterator(gNeoDatabase, getListClassID(), getListRoot(FALSE), nil, TRUE, kJokeID);
-
- // Randomly pick a joke
- iterator.leap(aIndex);
- joke = (CJoke *)iterator.currentObject();
-
- // Iterators don't add references to objects. So we add one ourselves.
- if (joke)
- joke->referTo();
-
- return joke;
- }
-
- void CJoker::learnJoke(CJoke *aJoke)
- {
- // Add the joke to this joker's part list
- addToList(aJoke);
- }
-
- /* ****************************************************************** */
- /** CPie Class **/
- /* ****************************************************************** */
- CPie::CPie(const char *aFilling)
- {
- setFilling(aFilling);
- }
-
- CNeoPersist *CPie::New(void)
- {
- return new CPie();
- }
-
- NeoID CPie::getClassID(void) const
- {
- return kPieID;
- }
-
- long CPie::getFileLength(void) const
- {
- return kNeoPersistFileLength + kMaxFillingName;
- }
-
- #pragma segment NeoRead
- void CPie::readObject(CNeoStream *aStream, const NeoTag aTag)
- {
- inherited::readObject(aStream, aTag);
-
- aStream->readString(fFilling, sizeof(fFilling));
- }
-
- #pragma segment NeoWrite
- void CPie::writeObject(CNeoStream *aStream, const NeoTag aTag)
- {
- inherited::writeObject(aStream, aTag);
-
- aStream->writeString(fFilling, sizeof(fFilling));
- }
-
- /* ****************************************************************** */
- /** CClown Class **/
- /* ****************************************************************** */
- CClown::CClown(const CNeoString &aName, const long aShoeSize)
- : CPerson(aName)
- {
- setObjClassID(kPieID);
- fShoeSize = aShoeSize;
- }
-
- CNeoPersist *CClown::New(void)
- {
- return new CClown();
- }
-
- NeoID CClown::getClassID(void) const
- {
- return kClownID;
- }
-
- long CClown::getFileLength(void) const
- {
- return kPersonFileLength + sizeof(fShoeSize);
- }
-
- #pragma segment NeoRead
- void CClown::readObject(CNeoStream *aStream, const NeoTag aTag)
- {
- inherited::readObject(aStream, aTag);
-
- fShoeSize = aStream->readLong();
- }
-
- #pragma segment NeoWrite
- void CClown::writeObject(CNeoStream *aStream, const NeoTag aTag)
- {
- inherited::writeObject(aStream, aTag);
-
- aStream->writeLong(fShoeSize);
- }
-
- CPie *CClown::bakePie(const char *aFilling)
- {
- CPie * pie;
-
- // Create a new pie object having the proper type of filling.
- // The default filling type is Custard, my favorite!
- pie = new CPie(aFilling);
-
- // Add it to the database.
- // Note: An object ID is assigned automaticly by addObject.
- gNeoDatabase->addObject(pie);
-
- // Add the pie to this clown's part list
- addToList(pie);
-
- return pie;
- }
-
- CPie *CClown::getPie(const long aIndex)
- {
- CPie * pie;
- CNeoPartListIterator iterator(gNeoDatabase, getListClassID(), getListRoot(FALSE), nil, TRUE, kPieID);
-
- // Randomly pick a pie
- iterator.leap(aIndex);
- pie = (CPie *)iterator.currentObject();
-
- // Iterators don't add references to objects. So we add one ourselves.
- if (pie)
- pie->referTo();
-
- return pie;
- }
-
- void CClown::skill(void)
- {
- long count = getPieCount();
- CPie * pie;
-
- if (count) {
- // Randomly pick a pie
- pie = getPie((rand()&0x7FFFFFFF) % count);
- NeoAssert(pie);
-
- printf("Throws pies: ");
- throwPie(pie);
-
- // Don't forget to remove our reference to the pie.
- pie->unrefer();
- }
- else
- printf("Has no pies to throw.\n");
- }
-
- void CClown::throwPie(CPie *aPie)
- {
- char filling[kMaxFillingName];
-
- // Remove the pie from this clown's part list
- deleteFromList(aPie);
-
- // Now remove it from the database completely.
- gNeoDatabase->removeObject(aPie);
-
- // Throw it!
- aPie->getFilling(filling);
- printf("Here's a %s pie in your face!\n", filling);
- }
-
-